package solution

type ListNode struct {
	Val  int
	Next *ListNode
}

func V1(l1 *ListNode, l2 *ListNode) *ListNode {
	var (
		l3   *ListNode
		prev *ListNode
	)

	for l1 != nil || l2 != nil {
		if (l1 != nil && l2 == nil) || (l1 != nil && l2 != nil && l1.Val <= l2.Val) {
			if prev == nil {
				l3 = &ListNode{Val: l1.Val}
				prev = l3
			} else {
				prev.Next = &ListNode{Val: l1.Val}
				prev = prev.Next
			}

			l1 = l1.Next
		} else {
			if prev == nil {
				l3 = &ListNode{Val: l2.Val}
				prev = l3
			} else {
				prev.Next = &ListNode{Val: l2.Val}
				prev = prev.Next
			}

			l2 = l2.Next
		}
	}

	return l3
}
